var modelAddModule = angular.module('wfModuleAddApp', []);
modelAddModule
		.controller(
				'wfModuleAddCtr',
				[
						'$scope',
						'$http',
						function($scope, $http) {
							$scope.id = angular.element($("#id")).val(); // 获取id
							// alert( $scope.id);
							$scope.getOrganizaAdd = function() {
								$('#organizaAdd')
										.modal(
												{
													remote : basePath
															+ '/basicData/department/department_wfmodule_selList.jsp'
												})
							};
							$scope.clearSel = function() {
								$('#depName').val('');
								$('#depCode').val('');
							};
							$scope.init = function() {
								$http(
										{
											url : basePath
													+ '/flowManager/queryFlowMapForId',
											method : 'GET',
											params : {
												"id" : angular
														.element($("#id"))
														.val()
											}
										})
										.success(
												function(data, header, config,
														status) {
													$scope.depName = data.depName; // 部门名称
													$scope.depCode = data.depId; // 部门ID
													$scope.moduleName = data.moduleCode; // 业务模块
													if (data.moduleCode == "SJ") {
														$scope.moduleName = "商机";
													}else if(data.moduleCode=="BJD"){
														$scope.moduleName ="报价单";
													}else if(data.moduleCode=="SQ"){
														$scope.moduleName ="授权审核";
													}else if(data.moduleCode=="CPDJ"){
														$scope.moduleName ="产品定价";
													}else if(data.moduleCode=="SSFWDJ"){
														$scope.moduleName ="实施服务人天定价";
													}else if(data.moduleCode=="HTSKD"){
														$scope.moduleName ="合同收款单";
													}else if(data.moduleCode=="CP"){
														$scope.moduleName ="产品";
													}else if(data.moduleCode == "DHD"){
														$scope.moduleName = "订货单";
													}else if(data.moduleCode == "FP"){
														$scope.moduleName = "发票";
													}else if(data.moduleCode == "HT"){
														$scope.moduleName = "合同";
													}
													$scope.flowName = data.flowName;// 流程名称

												}).error(
												function(data, header, config,
														status) {
													toastr.warning("操作失败！");
												});
							};
							$scope.init();
							// 点击保存按钮，新增一条数据
							$scope.saveWFModule = function() {
								// form表单的验证 数据不对，return false
								var formValid = $scope.wfModuleFormEdit.$valid;

								$scope.depName = $('#depName').val();
								$scope.depCode = $('#depCode').val();

								if (formValid) { // 表单通过验证后提交
									submitData();
									// submitData();//提交
								}
							};

							// form表单提交方法 submit(若是校验都通过啦，数据便可以提交啦)
							function submitData() {
								$http(
										{
											url : basePath
													+ '/flowManager/updateFlowDepRelation',
											method : 'POST',
											params : {
												'id' : $scope.id, // 流程Id
												'depId' : $scope.depCode, // 部门Id
												'depName' : $scope.depName, // 部门名称

											}
										})
										.success(
												function(data, header, config,
														status) {
													if (data.rmsg === 0) {
														toastr.success("操作失败");
													} else {
														toastr.success("操作成功");
													}
													setTimeout(
															function() {
																location.href = basePath
																		+ '/system/flowManager/wfmodule_list.jsp';
															}, 500);
												})
										.error(
												function(data, header, config,
														status) {
													toastr.error("调用错误！");
													setTimeout(
															function() {
																location.href = basePath
																		+ '/system/flowManager/wfmodule_list.jsp';
															}, 500);
												});
							}

							// 点击取消按钮，跳转到查询页面
							$scope.backHis = function() {
								location.href = basePath
										+ '/system/flowManager/wfmodule_list.jsp';
							};
						} ]);